/*
 * KKCanvas.cpp
 *
 *  Created on: 2013-9-23
 *      Author: wanghua
 */

#include "KKCanvas.h"
#include "XmlUtils.h"

using namespace std;

USING_NS_CC;

KKCanvas::KKCanvas() {
	m_strokes = new KKStrokeArray(4);
	m_strokeWidth = 20;
	m_currentStroke = NULL;
	m_strokeColor = ccc4(255, 255, 255, 255);
}

KKCanvas::~KKCanvas() {
	CC_SAFE_DELETE(m_strokes);
	m_currentStroke = NULL;
}

bool KKCanvas::initCommonInfoWithXml(TiXmlElement* element, KKNode* parent) {

	if (KKLayer::initCommonInfoWithXml(element, parent)) {

		this ->m_pen = XmlUtils::getStringAttr(element, "pen", "stroke.png");
		this ->m_strokeWidth = XmlUtils::getIntAttr(element, "strokeWidth", 20);

		ccColor3B strokeRgb = strToColor3(
				XmlUtils::getStringAttr(element, "strokeColor", "ffffff"));

		this ->m_strokeColor.r = strokeRgb.r;
		this->m_strokeColor.g = strokeRgb.g;
		this->m_strokeColor.b = strokeRgb.b;
		this->m_strokeColor.a = XmlUtils::getIntAttr(element, "strokeAlpha",
				255);

		return true;
	}

	return false;
}

KKCanvas * KKCanvas::canvasWithTarget(KKNode* target, const char* penfile) {

	KKCanvas* canvas = new KKCanvas();

	canvas->setContentSize(CCSize(target->getWidth(), target->getHeight()));

	canvas->autorelease();

	CCNode* renderArea = dynamic_cast<CCNode*> (target);

	canvas->ignoreAnchorPointForPosition(true);


	canvas->setAnchorPoint(ccp(0,0));

	canvas->setPosition(0, 0);

	renderArea->addChild(canvas, 100, 100);

	canvas->setPen(penfile);

	return canvas;

}

void KKCanvas::newStroke(CCPoint start) {

	m_currentStroke = KKRibbon::ribbonWithWidth(m_strokeWidth, m_pen, 1000,
			m_strokeColor, 1);

	 m_currentStroke->addPointAt(start, m_strokeWidth);

	this->addChild(m_currentStroke);
	this->m_strokes->addObject(m_currentStroke);

	m_strokePoints.clear();

	m_strokePoints.push_back(start);
}

void KKCanvas::easeStroke() {

	m_strokePoints.clear();

	this->removeChild(m_currentStroke, false);
	m_strokes->removeLastObject(true);
	m_currentStroke = this->m_strokes->getLastObject();
}

void KKCanvas::addPoint(CCPoint end) {
	m_currentStroke->addPointAt(end, m_strokeWidth);

	m_strokePoints.push_back(end);
}

void KKCanvas::clear() {

	m_strokePoints.clear();

	this->removeAllChildrenWithCleanup(false);
	this->m_strokes->removeAllObjects(true);
	m_currentStroke = NULL;
}

bool KKCanvas::saveImage(const char *szFilePath, bool allCanvas) {

	CCRenderTexture
			* renderTexture =
					CCRenderTexture::create(
							this->getContentSize().width,
							this->getContentSize().height);

	renderTexture->clear(0, 0, 0, 0);

	renderTexture->begin();

	if (allCanvas) {

		CCPoint pos = this->getPosition();

		this->setPosition(0,0);

		this->visit();

		this->setPosition(pos);

	} else {
		for (unsigned int i = 0; i < m_strokes->count(); i++) {
			m_strokes->getObjectAtIndex(i)->visit();
		}
	}

	renderTexture->end();

	return renderTexture->saveToFile(szFilePath);
}

void KKCanvas::onScroll(const KKTouch& touch, const CCPoint& dir) {
	addPoint(touch.getPoint());
	KKLayer::onScroll(touch, dir);
}

void KKCanvas::onDown(const KKTouch& touch) {
	newStroke(touch.getPoint());
	KKLayer::onDown(touch);
}

void KKCanvas::onUp(const KKTouch& touch) {
	KKLayer::onUp(touch);
}

